import React from "react";
import BaseComponent from "../../../../redare/components/BaseComponent";
import MenuPresenter from "../../.gen/presenter/component/core/MenuPresenter";
import TreeSelect from "../../../../redare/components/common/TreeSelect";

export default class ParentMenuSelect extends BaseComponent {

    static defaultProps = {
        menuId: undefined,
        platformMenuId: undefined,
        onValueChange: undefined,
        style: undefined
    };

    constructor(props) {
        super(props);
        this.state.value = this.props.value;
        this.state.data = [];
        this.presenter = new MenuPresenter(this);
    }

    componentDidMount() {
        this._findMenuList();
    }

    _findMenuList() {
        let {menuId, platformMenuId} = this.props;
        this.presenter.findMenuList({
            excMenuAndChildrenByMenuId: menuId,
            platformMenuId,
            sortToTree: false
        }, {returnAll: true}, (result) => {
            this.setState({
                data: result.data
            })
        });
    }

    _onValueChange(value) {
        this.setState({value});
        if (this.props.onValueChange) {
            this.props.onValueChange(value);
        }
    }

    render() {
        return (
            <TreeSelect allowClear={true}
                        treeData={this.state.data}
                        value={this.state.value}
                        multiple={false}
                        showCheckedStrategy={TreeSelect.SHOW_PARENT}
                        onChange={this._onValueChange.bind(this)}
                        style={this.props.style}
                        treeDataSimpleMode={true}
                        _simpleTreeDataPidFun={(record) => record.parentMenuId}
                        _simpleTreeDataIdFun={(record) => record.menuId}
                        _treeDataTitleFun={(record) => record.name}
                        _treeDataValueFun={(record) => record.menuId}
                        _treeDataKeyFun={(record) => record.menuId}
            >
            </TreeSelect>
        )
    }
}
